﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc; 
using System.IO;
using Wedo.Recognition.Web.Controllers;
using Wedo.Recognition.Domain.Models;
using Wedo.Mvc.Utility;
using Wedo.Recognition.Service;
using Wedo.Mvc.Pager;
using Wedo.Domain.Models;
using System.Data.Entity;
using Wedo.Recognition.Domain;
using Wedo.Recognition.Repository;
namespace Wedo.IPRP.Web.Controllers
{
    public class DisputeApplyController : EntityController<DisputeApply, CommonSearchModel>
    {
        private BankDataService dbService;
        private DisputeApplyService service;
        public DisputeApplyController(IUnitWork dbContext)
            : base(dbContext)
        {

            dbService = new BankDataService(_dbContext);
            service = new DisputeApplyService(_dbContext);
        }
        /// <summary>
        /// 等待审批的争议申请
        /// </summary>
        /// <returns></returns>
        public override ActionResult Index()
        {

            return Search(new CommonSearchModel ());
        }

        /// <summary>
        /// 我的申请记录
        /// </summary>
        /// <returns></returns>
        public ActionResult MyDisputeApply()
        {
            var where = new CommonSearchModel { ApplyMan = CurrentUserName };
            var list = GetSearchPagedList(where);
            
            ViewData["SearchModel"] = where; 
            return ReturnAjaxView("Index", list);
 
        }

        /// <summary>
        /// 我的未处理的申请记录
        /// </summary>
        /// <returns></returns>
        public ActionResult MyWaitedDisputeApply()
        {
            var where = new CommonSearchModel { ApplyMan = CurrentUserName, Status=1 };
            var list = GetSearchPagedList(where);

            ViewData["SearchModel"] = where;
            return ReturnAjaxView("Index", list);
        }

        /// <summary>
        /// 审批通过争议申请
        /// </summary>
        /// <param name="lid">争议记录编号</param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult Agree(Guid id,FormCollection forms)
        {
            ResultMSG res = new ResultMSG();
            try
            {
                service.ApproveDispute(id, CurrentUserName);
                res.Result = true;
                res.MSG = Wedo.Recognition.Resources.ErrorMessage.DisputeApproval;
            }
            catch (Exception ex)
            {
                res.Result = false;
                res.MSG = ex.Message;
            }
            return Json(res);
        }

        //[HttpGet]
        //public ActionResult Agree(Guid id)
        //{
        //    var model = FindById(id);
        //    return ReturnAjaxView(model);
        //}

        /// <summary>
        /// 审批拒绝争议申请
        /// </summary>
        /// <param name="lid">争议记录编号</param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult Reject(Guid id)
        {
            ResultMSG res = new ResultMSG(); 
            try
            {
                service.RejectDispute(id, CurrentUserName);
                res.Result =true;
                res.MSG = Wedo.Recognition.Resources.ErrorMessage.DisputeApproval;
            }
            catch (Exception ex)
            {
                res.Result =false;
                res.MSG = ex.Message;
            }
            return Json(res);
        }

        /// <summary>
        /// 撤销争议申请
        /// </summary>
        /// <param name="lid">争议记录编号</param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult Cancel(Guid id)
        {
            ResultMSG res = new ResultMSG(); 
            try
            {
                service.CancelDisputeItem(id, CurrentUserName);
                res.Result =true;
                res.MSG = Wedo.Recognition.Resources.ErrorMessage.DisputeApproval;
            }
            catch (Exception ex)
            {
                res.Result =false;
                res.MSG = ex.Message;
            }
            return Json(res);
        }

        /// <summary>
        /// 显示争议申请的具体项
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult DisputeApplyItems(Guid id)
        { 
            DisputeApply item = FindById(id);
            return Json(item);
        }

        public ActionResult SimpleDisputeList(Guid id)
        {
            var list = service.GetAll(m => m.BankDataId == id).ToList();
            return ReturnAjaxView(list);
        }

        protected override Wedo.Service.Common.IBaseService<DisputeApply> EntityService
        {
            get { return service; }
        }

        protected override DisputeApply FindById(Guid Id)
        {
            return service.GetAll(m => m.Id == Id,"BankData").FirstOrDefault();
        }


        protected override PagedList<DisputeApply> GetSearchPagedList(CommonSearchModel where)
        {

            where = where ?? new CommonSearchModel();

            var query = service.GetAll(includes: "BankData");
            if (where.StartTime != null)
            {
                DateTime Start = where.StartTime.Value;
                query = query.Where(m => m.ApplyTime > Start);
            }
            if (where.EndTime != null)
            {
                DateTime endTime = where.EndTime.Value.AddDays(1);
                query = query.Where(m => m.ApplyTime < endTime);
            }

            if (where.Status != null)
                query = query.Where(m => m.Status == where.Status);

            if (where.ApplyMan.IsNotNullOrWhiteSpace())
            {
                query = query.Where(m => m.ApplyMan == where.ApplyMan);
            }
            if (where.Keyword.IsNotNullOrWhiteSpace())
            {
                query = query.Where(m => m.BankData.Assiginment.Contains(where.Keyword));
            }

            return query.OrderBy(m => m.Status).ThenByDescending(m => m.Created).ToPagedList(where.Page, 10);
        }
 
    }
}
